
#include <iostream>
#include "http.hpp"

#define WWWROOT "./wwwroot/"

std::string RequestStr(const HttpRequest &req) {
    std::stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for (auto &it : req._params) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    for (auto &it : req._headers) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    ss << "\r\n";
    ss << req._body;
    return ss.str();
}
void Hello(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
    sleep(40);
}
void Login(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
}
void PutFile(const HttpRequest &req, HttpResponse *rsp) 
{
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname, req._body);
}
void DelFile(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
}

int main ()
{
    HttpServer server(8150);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT);//设置静态资源根目录，告诉服务器有静态资源请求到来，需要到哪里去找资源文件
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);
    server.Delete("/1234.txt", DelFile);
    server.Listen();
    return 0;
}



































// //根据文件后缀名获取文件mime
// static std::string ExMime(const std::string &filename){
//     std::unordered_map<std::string,std::string> _mime_msg = {
//         {".aac",        "audio/aac"},
//         {".abw",        "application/x-abiword"},
//         {".arc",        "application/x-freearc"},
//         {".avi",        "video/x-msvideo"},
//         {".azw",        "application/vnd.amazon.ebook"},
//         {".bin",        "application/octet-stream"},
//         {".bmp",        "image/bmp"},
//         {".bz",         "application/x-bzip"},
//         {".bz2",        "application/x-bzip2"},
//         {".csh",        "application/x-csh"},
//         {".css",        "text/css"},
//         {".csv",        "text/csv"},
//         {".doc",        "application/msword"},
//         {".docx",       "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
//         {".eot",        "application/vnd.ms-fontobject"},
//         {".epub",       "application/epub+zip"},
//         {".gif",        "image/gif"},
//         {".htm",        "text/html"},
//         {".html",       "text/html"},
//         {".ico",        "image/vnd.microsoft.icon"},
//         {".ics",        "text/calendar"},
//         {".jar",        "application/java-archive"},
//         {".jpeg",       "image/jpeg"},
//         {".jpg",        "image/jpeg"},
//         {".js",         "text/javascript"},
//         {".json",       "application/json"},
//         {".jsonld",     "application/ld+json"},
//         {".mid",        "audio/midi"},
//         {".midi",       "audio/x-midi"},
//         {".mjs",        "text/javascript"},
//         {".mp3",        "audio/mpeg"},
//         {".mpeg",       "video/mpeg"},
//         {".mpkg",       "application/vnd.apple.installer+xml"},
//         {".odp",        "application/vnd.oasis.opendocument.presentation"},
//         {".ods",        "application/vnd.oasis.opendocument.spreadsheet"},
//         {".odt",        "application/vnd.oasis.opendocument.text"},
//         {".oga",        "audio/ogg"},
//         {".ogv",        "video/ogg"},
//         {".ogx",        "application/ogg"},
//         {".otf",        "font/otf"},
//         {".png",        "image/png"},
//         {".pdf",        "application/pdf"},
//         {".ppt",        "application/vnd.ms-powerpoint"},
//         {".pptx",       "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
//         {".rar",        "application/x-rar-compressed"},
//         {".rtf",        "application/rtf"},
//         {".sh",         "application/x-sh"},
//         {".svg",        "image/svg+xml"},
//         {".swf",        "application/x-shockwave-flash"},
//         {".tar",        "application/x-tar"},
//         {".tif",        "image/tiff"},
//         {".tiff",       "image/tiff"},
//         {".ttf",        "font/ttf"},
//         {".txt",        "text/plain"},
//         {".vsd",        "application/vnd.visio"},
//         {".wav",        "audio/wav"},
//         {".weba",       "audio/webm"},
//         {".webm",       "video/webm"},
//         {".webp",       "image/webp"},
//         {".woff",       "font/woff"},
//         {".woff2",      "font/woff2"},
//         {".xhtml",      "application/xhtml+xml"},
//         {".xls",        "application/vnd.ms-excel"},
//         {".xlsx",       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
//         {".xml",        "application/xml"},
//         {".xul",        "application/vnd.mozilla.xul+xml"},
//         {".zip",        "application/zip"},
//         {".3gp",        "video/3gpp"},
//         {".3g2",        "video/3gpp2"},
//         {".7z",         "application/x-7z-compressed"}
//     };
//     //a.b.txt 先获取文件扩展名
//     size_t pos = filename.find_last_of(".");
//     if(pos == std::string::npos){
//         return "application/octet-stream";
//     }
//     //根据扩展名获取mine
//     std::string ext = filename.substr(pos);
//     auto it = _mime_msg.find(ext);
//     if(it == _mime_msg.end()){
//         return "application/octet-stream";
//     }
//     return it->second;
// }
// //判断一个文件是否是一个目录
// static bool IsDirectory(const std::string &filename){
//     struct stat st;
//     int ret = stat(filename.c_str(),&st);
//     if(ret < 0){
//         return false;
//     }
//     return S_ISDIR(st.st_mode);
// }
// //判断一个文件是否是一个普通文件
// static bool IsRegular(const std::string &filename){
//     struct stat st;
//     int ret = stat(filename.c_str(),&st);
//     if(ret < 0){
//         return false;
//     }
//     return S_ISREG(st.st_mode);
// }

// int main ()
// {
//     // std::cout << IsDirectory("./dir") << std::endl;
//     // std::cout << IsDirectory("./http.hpp") << std::endl;

//     // std::cout << ExMime("a.png") <<std::endl;
//     return 0;
// }